//
//  WelcomeViewController.swift
//  LVWeibo
//
//  Created by lv on 15/6/27.
//  Copyright © 2015年 lv. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

class WelcomeViewController: UIViewController {

    
    // 头像底部约束
    var bottomConstraint:NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置显示头像
        if let urlString = sharedUserAccount?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    
    //MARK:- 界面设置
    override func loadView() {
        view = UIView()
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(messageLable)
        
        // 自动布局
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        messageLable.ff_AlignHorizontal(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))
    }
    
    //MARK:- 界面动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        bottomConstraint?.constant = 200 - UIScreen.mainScreen().bounds.height
        
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { _ in
                self.view.layoutIfNeeded()
            
            }) { _ in
                
                UIView.animateWithDuration(1.0, animations: {
                    self.messageLable.alpha = 1.0
                    }, completion: { _ in
                        // 欢迎界面显示完毕后发送通知，进行转换显示控制器
                        // object = true 表示显示 MainViewController
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchMainInterfaceNotification, object: true)
                })
        }
    }
    
    //MARK:- 懒加载子控件
    // 头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    }()
    // 欢迎归来
    private lazy var messageLable:UILabel = {
        let mbl = UILabel()
        mbl.text = "欢迎回来"
        mbl.sizeToFit()
        mbl.alpha = 0
        return mbl
    }()
    // 背景图
    private lazy var backImageView: UIImageView = {
       let backView = UIImageView(image: UIImage(named: "ad_background"))
       backView.frame = UIScreen.mainScreen().bounds
        return backView
    }()
}
